package codeRandomThoughts.Test404左叶子之和;

import codeRandomThoughts.utils.TreeNode;

/**
 * 后序遍历,这样能先获取到左节点和右节点的返回值
 */
public class Solution2 {
    public int sumOfLeftLeaves(TreeNode root) {
        //递归在什么时候结束?
        if (root == null || (root.left == null && root.right == null)) {
            return 0;
        }
        int leftValue = sumOfLeftLeaves(root.left);
        int rightValue = sumOfLeftLeaves(root.right);

        //如果怕left是左叶子,那么leftValue就是左叶子的值
        if (root.left != null && root.left.left == null && root.left.right == null) {
            leftValue = root.left.val;
        }
        return leftValue + rightValue;
    }
}
